exports.get_lottery_number = function( player, req, resp, onHandled) {

    if( player.user.status.level < gConfGlobal.LotteryOpenLevel ) {
        resp.code = 1; 
        resp.desc = 'level not reach'; 
        onHandled(); 
        return;
    }

    req.args.server = config.ServerId;
    requestCloud( req, resp, function() {
        if( 0 == resp.code ) {
            var user = player.user;
            var lottery = user.lottery;
            var today = common.getDate();
            if( today != lottery.day ) {
                lottery.day = today;
                lottery.count = 0;
                lottery.got = 0;
                player.markDirty( "lottery.day");
                player.markDirty( "lottery.count");
                player.markDirty( "lottery.got");
            } 
            
            resp.data.lottery = lottery;
            
            // 超级奖获奖信息公告
            var superInfos = resp.data.super_infos;
            var onePrize = resp.data.one_prize;
            if( onePrize && superInfos && (Object.keys(superInfos).length > 0) ) {
                for( var name in superInfos ) {
                    var count = superInfos[name];
                    var prize = onePrize * count;
                    gMessage.addSystemMessage('lottery_super',[ name, prize ]);
                } 
            }

            delete resp.data.super_infos;

            onHandled();             
        }
    }); 
}

exports.lottery_number_reward = function( player, req, resp, onHandled) {
    var user = player.user; 
    var lottery = user.lottery;
    do {
        if( user.status.level < gConfGlobal.LotteryOpenLevel ) {
            resp.code = 1; resp.desc = 'level not reach'; break; 
        }

        var today = common.getDate();
        if( today != lottery.day ) {
            resp.code = 1; resp.desc = 'day invalid'; break; 
        }

        var now = common.getTime();
        var todayDate = new Date();
        var todayStartTime = +(new Date(todayDate.getFullYear(), todayDate.getMonth(), todayDate.getDate(), 0, 0, 0)) / 1000;
        var todayEndTime = +(new Date(todayDate.getFullYear(), todayDate.getMonth(), todayDate.getDate(), 23, 59, 59)) / 1000;
        var openStartTime = todayStartTime + gConfGlobal.LotteryOpenHour * 3600;
        if( (openStartTime > now) || (now > todayEndTime) ) {
            resp.code = 1; resp.desc = 'can not reward prize'; break; 
        }

        if( (lottery.count <= 0) || lottery.got ) {
            resp.code = 1; resp.desc = 'have got'; break; 
        }
   }while(false);

   if( resp.code != 0) {
       onHandled();
       return;
   }

    requestCloud( req, resp, function() {
        if( resp.code == 0 ){
            var prizeCash = resp.data.prize_cash;
            if( prizeCash ) {
                player.alterResource('cash', Math.floor(prizeCash));
                lottery.got = 1;
                player.markDirty('lottery.got');
            } 
        } 
        onHandled();
    });
}

exports.lottery_shop_buy = function(player ,req, resp, onHandled) {
    do {
        if( player.user.status.level < gConfGlobal.LotteryOpenLevel ) {
            resp.code = 1; resp.desc = 'level not reach'; break; 
        }

        if( isNaN(req.args.id) || isNaN(req.args.num) ) {
            resp.code = 1; resp.desc = 'no valid args'; break; 
        }
        
        var id = Math.floor(req.args.id);
        var num = Math.floor(req.args.num);
        if( num <= 0) {
            resp.code = 1; resp.desc ='invalid num'; break;
        }
        
        var user = player.user;
        var lottery = user.lottery;

        var lotteryShopConf = gConfLotteryShop[id];
        if( !lotteryShopConf ) {
            resp.code = 1; resp.desc = 'invalid id'; break;
        }
        
        var costScore = lotteryShopConf.Score * num;
        if( lottery.score < costScore ) {
            resp.code = 1; resp.desc = 'not enough score';break; 
        }

        lottery.score -= costScore; 
        player.markDirty('lottery.score');

        var awards = [];
        for( var i = 1; i <= num; i++ ) {
            awards.push(lotteryShopConf.Award1); 
        }

        resp.data.awards = player.addAwards( awards );
        resp.data.score = -costScore;
    }while(false);

    onHandled();
}
